#include "Window.h"

#include <stdexcept>

namespace vkengine
{
	Window::Window(int w, int h, std::string name) :m_Width{ w }, m_Height{ h }, m_WindowName{ name }
	{
		InitWindow();
	}

	Window::~Window()
	{
		glfwDestroyWindow(m_Window);
		glfwTerminate();
	}

	void Window::InitWindow()
	{
		glfwInit();
		glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
		glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

		m_Window = glfwCreateWindow(m_Width, m_Height, m_WindowName.c_str(), nullptr, nullptr);
		glfwSetWindowUserPointer(m_Window, this);
		glfwSetFramebufferSizeCallback(m_Window, FramebufferResizeCallback);
	}

	void Window::CreateWindowSurface(VkInstance instanc, VkSurfaceKHR* surface)
	{
		if (glfwCreateWindowSurface(instanc, m_Window, nullptr, surface) != VK_SUCCESS)
		{
			throw std::runtime_error("failed to create window surface");
		}
	}

	void Window::FramebufferResizeCallback(GLFWwindow* pwindow, int width, int height)
	{
		auto window = reinterpret_cast<Window*>(glfwGetWindowUserPointer(pwindow));
		window->m_FramebufferResized = true;
		window->m_Width = width;
		window->m_Height = height;
	}
}
